import {
  Form
} from 'antd-mini/es/Form/form';
import {
  workerLastReviewOrderSubmit,
  workerPreReviewOrderSubmit,
  workerInitReviewOrderSubmit
} from '/service/service';
import {
  workOderAudit
} from '/service/service'

Page({
  data: {
    id: null,
    data: null,
    form: null,
    auditResult: '',
    auditResultOptions: [{
        value: '通过',
        text: '通过'
      },
      {
        value: '退回',
        text: '退回'
      },
    ],
    isCloseCaseOptions: [{
        value: '是',
        text: '是'
      },
      {
        value: '否',
        text: '否'
      },
    ],
    isOrderTakingOverTimeOptions: [{
        value: '是',
        text: '是'
      },
      {
        value: '否',
        text: '否'
      },
    ],
    isHandleOverTimeOptions: [{
        value: '是',
        text: '是'
      },
      {
        value: '否',
        text: '否'
      },
    ],
    isCloseCase: '',
    isShowCloseCaseBtn: true,
    status: -1,

    isOverTimeOfHandleOverTime: false,
    isOverTimeOfOrderTaking: false,
    isEditOverTimeSetting: false
  },
  onLoad(option) {
    const {
      item
    } = option
    const {
      id,
      status
    } = JSON.parse(item);
    // console.log(id, status)
    if (!id || !status) {
      my.showToast({
        content: '传参错误，请检查',
        type: 'fail'
      });
      return;
    }
    this.getInfo(id)
    let isShowCloseCaseBtn = this.data.isShowCloseCaseBtn
    if (status === 14) {
      isShowCloseCaseBtn = false
    }
    if ([4, 5].includes(status)) {
      this.setData({
        isEditOverTimeSetting: true
      })
    }
    this.setData({
      id,
      status,
      isShowCloseCaseBtn
    });

    this.form = new Form({
      initialValues: {},
      rules: {
        auditResult: [{
          required: true,
          message: '请选择审核结果',
        }],
        auditOpinion: [{
          required: true,
          message: '请输入审核意见',
        }, ],
        isOrderTakingOverTime: [{
          required: true,
          message: '请选择是否接单超期',
        }, ],
        orderTakingCost: [{
          required: true,
          message: '请输入接单超期扣款',
        }, ],
        isHandleOverTime: [{
          required: true,
          message: '请选择是否处理超期',
        }, ],
        handleCost: [{
          required: true,
          message: '请输入处理超期扣款',
        }, ],
      },
    })
  },

  async getInfo(id) {
    try {
      const data = await workOderAudit(id)
      console.log('接口返回数据:', data)
      this.setData({
        data
      })

      const {
        isHandleOverTime,
        isOrderTakingOverTime
      } = data
      this.setData({
        isOverTimeOfHandleOverTime: isHandleOverTime === '是' ? true : false,
        isOverTimeOfOrderTaking: isOrderTakingOverTime === '是' ? true : false
      })
      this.setFormValues(data)
    } catch (error) {
      console.error('获取数据失败:', error)
    }
  },
  setFormValues(data) {
    if (!this.form) return
    const formValues = {
      ...data
    }
    this.form.setFieldsValue(formValues)
  },

  handleRef(ref) {
    // console.log(ref.props.name)
    if (this.form && ref) {
      this.form.addItem(ref);

      // // 如果已有数据，立即同步到该字段
      // if (this.data.data) {
      //   const fieldName = ref.props.name
      //   this.form.setFieldsValue({
      //     [fieldName]: this.data.data[fieldName] || ''
      //   })
      // }
    }
  },
  async submit() {
    try {
      const values = await this.form.submit()

      const params = {
        ...values,
        id: this.data.id
      }
      const {
        status
      } = this.data
      let res = null
      if (status === 4) {
        res = await workerInitReviewOrderSubmit(params)
      } else if (status === 5) {
        res = await workerLastReviewOrderSubmit(params)
      } else if (status === 14) {
        res = await workerPreReviewOrderSubmit(params)
      } else {
        my.showToast({
          content: '提交失败，工单状态已变更',
          type: 'fail',
          duration: 2000
        })
        return
      }

      // console.log(res)
      if (res && res.code === 200) {
        my.showToast({
          content: res.msg,
          type: 'success',
          duration: 2000
        })
        my.navigateBack()
      } else {
        my.showToast({
          content: '提交失败，请检查',
          type: 'fail',
          duration: 2000
        })
      }
    } catch (e) {
      my.showToast({
        content: '提交异常，请检查',
        type: 'fail',
        duration: 2000
      })
    }
  },
  auditResultChange(e) {
    this.setData({
      auditResult: e
    })
  },
  isOrderTakingOverTimeChange(e) {
    if (e === '是') {
      this.setData({
        isOverTimeOfOrderTaking: true
      })
    }
    this.form.setFieldsValue('isOrderTakingOverTime', e)
  },
  isHandleOverTimeChange(e) {
    if (e === '是') {
      this.setData({
        isOverTimeOfHandleOverTime: true
      })
    }
    this.form.setFieldsValue('isHandleOverTime', e)
  },
  cancel() {
    my.navigateBack();
  }
});